package view.item
{
	import control.manager.BitmapManager;
	import control.manager.LayerManager;
	import control.util.CustomLoader;
	
	import data.BitmapItem;
	import data.GameConst;
	import data.MapData;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.LoaderInfo;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.net.URLRequest;
	
	import view.ui.UIMain;

	/**
	 * 地图块，会自动加载相应的图片 
	 * @author Leo
	 * 
	 */	
	public class MapTileItem extends BitmapItem
	{
//		/**
//		 * 地图块对应的唯一uid值 
//		 */		
//		public var uid :String;
//		/**
//		 * 是否已经加载完成 
//		 */		
//		public var isComplete :Boolean;
		/**
		 * 加载器 
		 */		
		private var loader :CustomLoader;
		/**
		 * 行 
		 */		
		public var row :int;
		/**
		 * 列 
		 */		
		public var col :int;
		/**
		 * 地图数据 
		 */		
		public var mapData :MapData;
		/**
		 * 是否是缩略图 
		 */		
		public var isThumbnail :int;
		
		/**
		 * 地图块，会自动加载相应的图片
		 * @param mapID
		 * @param row
		 * @param col
		 * @param isThumbnail 是否缩略图
		 * 
		 */		
		public function MapTileItem(mapData :MapData, row :int = 0, col :int = 0, isThumbnail :int = 0)
		{
			super();
			
			this.row = row;
			this.col = col;
			this.mapData = mapData;
			this.isThumbnail = isThumbnail;
			this.isComplete = false;
			this.isLoading = true;
			switch(isThumbnail)
			{
				//地图块
				case 0:
					loadTile();
					break;
				//地图缩略图
				case 1:
					loadThumbnail();
					break;
			}
			

			BitmapManager.instance.mapTileItemList.push(this);
		}
		/**
		 * 加载地图块 
		 * 
		 */		
		private function loadTile():void
		{
			var url :String  = GameConst.mapSource + this.mapData.mapID + "/" + row + "_" + col + ".jpg";
			this.uid = "map_" + this.mapData.mapID + "_" + row + "_" + col;
			loader  = new CustomLoader();
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE,completeHandler);
			loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,errorHandler);
			loader.load(new URLRequest(url));
		}
		/**
		 * 加载地图缩略图 
		 * 
		 */		
		private function loadThumbnail():void
		{
			var url :String = GameConst.mapSource + this.mapData.mapID + "/" + "pre.jpg";
			this.uid = "map_" + this.mapData.mapID + "_" + "pre";
			loader  = new CustomLoader();
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE,completeHandler);
			loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,errorHandler);
			loader.load(new URLRequest(url));
		}
		
		private function completeHandler(event :Event):void
		{
			loader.contentLoaderInfo.removeEventListener(Event.COMPLETE,completeHandler);
			loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR,errorHandler);
			this.bitmapData = (this.loader.content as Bitmap).bitmapData;

			
			switch(isThumbnail)
			{
				//地图块
				case 0:
					x = this.col * this.mapData.mapTileWidth;
					y = this.row * this.mapData.mapTileHeight;
					break;
				//地图缩略图
				case 1:
//					width = this.mapData.mapWidth;
//					height = this.mapData.mapHeight;
//					this.scaleX = this.mapData.mapWidth/this.bitmapData.width;
//					this.scaleY = this.mapData.mapHeight/this.bitmapData.height;
					
					var mainUI :UIMain = LayerManager.instance.getLayer(UIMain.NAME) as UIMain;
					mainUI.miniMapContainer.miniMap.bitmapData = this.bitmapData;
					break;
			}
//			this.cacheAsBitmap = true;
			this.loader.unloadAndStop();
			this.loader = null;
			this.isComplete = true;
			this.isLoading = false;
		}
		/**
		 * 加载地图块出错 
		 * @param event
		 * 
		 */		
		private function errorHandler(event :IOErrorEvent):void
		{
			loader.contentLoaderInfo.removeEventListener(Event.COMPLETE,completeHandler);
			loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR,errorHandler);
			trace("at MapTileItem 加载地图块出错" + loader.url);
			this.isComplete = false;
			this.isLoading = false;
			this.loader = null;
		}
	}
}